<?php
namespace api\modules\v1\models;

use common\models\Functions;
use common\models\Status;
use Yii;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class Versions extends \common\models\versions\Versions
{


    public function checkVersion(){
        
        $terminal = strtolower(Yii::$app->request->headers->get('version-terminal',''));
        $channel = strtolower(Yii::$app->request->headers->get('channel',''));
        $versionCode = strtolower(Yii::$app->request->headers->get('version-code',''));
        $where = [];

        if (!empty($channel)) {
            $where['channel'] = $channel;
        } else {
            $where['channel'] = "app";
        }

        # 最新版本信息
        $where['version_terminal'] = $terminal;
        $where['is_online'] = Status::COMMON_STATUS_SHOW;
        $lastVersion = static::getOneByWhere($where, '*', 'version_code DESC');
        $data['versionCode'] = intval($lastVersion['version_code']);
        $data['versionName'] = (string) $lastVersion['version_name'];
        $data['versionDesc'] = (string) $lastVersion['version_info'];
        $data['downloadUrl'] = (string) $lastVersion['download_url'];
        $data['downloadDirectUrl'] = (string) $lastVersion['direct_dw_url'];

        //是否强制升级
        $data['isForce'] = $lastVersion['is_force'] == 1 ? true : false;
        //系统是否正在维护
        $data['isMaintenance'] = $lastVersion['is_maintenance'] == 1 ? true : false;
//        $data['baidu'] = null;
        $data['baidu'] = empty($lastVersion['baidu']) ? null : $lastVersion['baidu'];

        # 当前版本信息
        $currVersion = static::getOneByWhere(['version_terminal' => $terminal, "version_code" => $versionCode]);

        $data['currVersion'] = [
            'isAudit' => $currVersion['is_audit'] == 1,
        ];


        //是否系统正在维护
        return Functions::formatRestAPI(0, '', $data);
    }
    public function getDownload(){
        $data = [
            "apk" => null,
            "baidu" => null,
            "ios" => null
        ];

        $where['channel'] = "app";
        $where['is_online'] = Status::COMMON_STATUS_SHOW;
        $where['version_terminal'] = "android";
        $android = static::getOneByWhere($where, "*", "version_code DESC");

        $data['apk'] = empty($android['direct_dw_url']) ? null : $android['direct_dw_url'];
        $data['baidu'] = empty($android['baidu']) ? null : $android['baidu'];

        $where['version_terminal'] = "ios";
        $ios = static::getOneByWhere($where, "*", "version_code DESC");

        $data['ios'] = empty($ios['download_url']) ? null : $ios['download_url'];

        //是否系统正在维护
        return Functions::formatRestAPI(0, '', $data);
    }

}